<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="号段1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part01">
              <j-dict-select-tag type="list" v-model="model.part01" dictCode="mdm_material_rule_type" placeholder="请选择号段1" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="号段2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part02">
              <j-dict-select-tag type="list" v-model="model.part02" dictCode="mdm_material_rule_type" placeholder="请选择号段2" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="号段3" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part03">
              <j-dict-select-tag type="list" v-model="model.part03" dictCode="mdm_material_rule_type" placeholder="请选择号段3" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="号段4" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part04">
              <j-dict-select-tag type="list" v-model="model.part04" dictCode="mdm_material_rule_type" placeholder="请选择号段4" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="号段5" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part05">
              <j-dict-select-tag type="list" v-model="model.part05" dictCode="mdm_material_rule_type" placeholder="请选择号段5" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="序列号长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seqLength">
              <a-input-number v-model="model.seqLength" placeholder="请输入序列号长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="日期格式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dateFmt" >
              <j-dict-select-tag type="list" v-model="model.dateFmt" dictCode="mdm_date_fmt" placeholder="请选择日期格式" :disabled="dateFmtDisabled" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="字符内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fixed">
              <a-input v-model="model.fixed" placeholder="请输入字符内容" :disabled="fixedDisabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-switch v-model="model.enableFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MdmMaterialCodeRuleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            part01:"04",
            part02:"01",
            part03:"02",
            part04:"03",
            part05:"05",
            seqLength:3,
            seqNo:1,
            enableFlag: '1',
            dateFmt: 'yyyyMMdd'
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          seqLength: [
            { required: false},
            { pattern: /^-?\d+$/, message: '请输入整数!'},
            { pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '请输入大于零的数' }
          ],
        },
        url: {
          add: "/mdm/materialCodeRule/add",
          edit: "/mdm/materialCodeRule/edit",
          queryById: "/mdm/materialCodeRule/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      dateFmtDisabled(){
        let partArr = [this.model.part01,this.model.part02,this.model.part03,this.model.part04,this.model.part05];
        return !partArr.includes('03')
      },
      fixedDisabled(){
        let partArr = [this.model.part01,this.model.part02,this.model.part03,this.model.part04,this.model.part05];
        return !partArr.includes('04')
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        // 校验
        let partArr = [this.model.part01,this.model.part02,this.model.part03,this.model.part04,this.model.part05];
        let newPartArr = partArr.filter(value => value === '05') || [];
        if(newPartArr.length == 0){
          this.$message.error("至少有一个号段是【自增序列】!")
          return
        }
        newPartArr = partArr.filter(value => value === '03') || [];
        if(newPartArr.length > 0 && !this.model.dateFmt){
          this.$message.error("请设定日期格式!")
          return
        }
        if(this.dateFmtDisabled){
          this.model.dateFmt = '';
        }
        if(this.fixedDisabled){
          this.model.fixed = '';
        }
        //
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>